package com.zzz.robot201.mvp.ui.activity;

import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.zzz.robot201.R;
import com.zzz.robot201.app.base.BaseViewActivity;
import com.zzz.robot201.app.config.AppContent;
import com.zzz.robot201.di.component.DaggerLoginComponent;
import com.zzz.robot201.mvp.contract.LoginContract;
import com.zzz.robot201.mvp.model.api.bean.PhoneCode;
import com.zzz.robot201.mvp.model.api.bean.Register;
import com.zzz.robot201.mvp.presenter.LoginPresenter;
import com.zzz.robot201.utils.CountDownTimerUtils;
import com.zzz.robot201.utils.PhoneUtil;
import com.zzz.robot201.utils.SpUtils;
import com.zzz.robot201.utils.ToastUtil;
import com.zzz.robot201.utils.UIUtils;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.jess.arms.utils.Preconditions.checkNotNull;


public class LoginActivity extends BaseViewActivity<LoginPresenter> implements LoginContract.View {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_code)
    TextView tvCode;
    @BindView(R.id.btn_back)
    Button btnBack;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_code)
    EditText etCode;
    @BindView(R.id.et_password)
    EditText etPassword;
    @BindView(R.id.btn_login)
    Button btnLogin;
    @BindView(R.id.tv_forgetpwd)
    TextView tvForgetpwd;
    @BindView(R.id.tv_goregister)
    TextView tvGoregister;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerLoginComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_login; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        setTitle(tvTitle, UIUtils.getString(R.string.title_login));
    }

    @Override
    public void showLoading() {
        showBaseLoading();
    }

    @Override
    public void hideLoading() {
        hideBaseLoading();
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ToastUtil.showToast(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void killMyself() {
//        System.exit(0);
            ActivityManager activityManager = (ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
            List<ActivityManager.AppTask> appTaskList = activityManager.getAppTasks();
            for (ActivityManager.AppTask appTask : appTaskList) {
                appTask.finishAndRemoveTask();

        }
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @OnClick({R.id.btn_back, R.id.tv_code, R.id.btn_login, R.id.tv_forgetpwd, R.id.tv_goregister})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.tv_code:
                String phone = etPhone.getText().toString();
                if (PhoneUtil.isMobileNO(phone)) {
                    if (mPresenter != null) mPresenter.getPhoneCode(phone, "regCode");
                } else {
                    ToastUtil.showToast(UIUtils.getString(R.string.phone_number));
                }
                break;
            case R.id.btn_login:
                String phoneNumber = etPhone.getText().toString();
                String passWord = etPassword.getText().toString();
                if (PhoneUtil.isMobileNO(phoneNumber)) {
                    if (!TextUtils.isEmpty(passWord)) {
                        if (mPresenter != null) mPresenter.Login(phoneNumber, passWord);
                    } else {
                        ToastUtil.showToast(UIUtils.getString(R.string.password));
                    }
                } else {
                    ToastUtil.showToast(UIUtils.getString(R.string.phone_number));
                }
                break;
            case R.id.tv_forgetpwd:

                break;
            case R.id.tv_goregister:
                launchActivity(new Intent(this, RegisterActivity.class));
                break;
            default:
                break;
        }
    }

    @Override
    public void phoneCode(PhoneCode code) {
        if (code.isSuccess()) {
            // 倒计时
            CountDownTimerUtils mCountDownTimerUtils = new CountDownTimerUtils(tvCode, 60000, 1000);
            mCountDownTimerUtils.start();
        }
    }

    @Override
    public void loginResult(Register login) {
        SpUtils.put(this, AppContent.User.INFO, login.getData());
        SpUtils.put(this,AppContent.SaveInfoKey.TOKEN,login.getData().getToken());
        SpUtils.put(this, AppContent.SaveInfoKey.ISLOGIN, "true");
//        startActivity(new Intent(this, EquipmentActivity.class));
        finish();
    }

}
